/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.policy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.node.INode;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.policy.entity.LabelItem;
import org.springblade.modules.policy.service.ILabelItemService;
import org.springblade.modules.policy.vo.LabelItemVO;
import org.springblade.modules.policy.wrapper.LabelItemWrapper;
import org.springblade.modules.system.entity.DictItem;
import org.springblade.modules.system.vo.DictItemVO;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 标签 控制器
 *
 * @author Blade
 * @since 2022-07-05
 */
@RestController
@AllArgsConstructor
@RequestMapping("/blade-policy/labelitem")
@Api(value = "标签", tags = "标签")
public class LabelItemController extends BladeController {

	private ILabelItemService labelItemService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入labelItem")
	public R<LabelItem> detail(LabelItem labelItem) {
		LabelItem detail = labelItemService.getOne(Condition.getQueryWrapper(labelItem));
		return R.data(detail);
	}

	/**
	 * 分页 标签
	 */
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "type", value = "字典类型", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入labelItem")
	public R<List<INode>> list(@RequestParam Map<String, Object> label) {
		@SuppressWarnings("unchecked")
		List<LabelItem> list = labelItemService.list(Condition.getQueryWrapper(label, LabelItem.class).lambda().orderByAsc(LabelItem::getSort));
		return R.data(LabelItemWrapper.build().listNodeVO(list));
	}

	/**
	 * 获取标签树形结构
	 *
	 * @return
	 */
	@GetMapping("/tree")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<LabelItemVO>> getTree(@ApiParam(value = "标签类型", required = false, defaultValue = "") @RequestParam String type) {
		List<LabelItemVO> tree;
		if (StringUtil.isNotBlank(type)) {
			tree = labelItemService.selectTree(type);
		} else {
			tree = labelItemService.tree();
		}
		return R.data(tree);
	}

	/**
	 * 自定义分页 标签
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入labelItem")
	public R<IPage<LabelItemVO>> page(LabelItemVO labelItem, Query query) {
		IPage<LabelItemVO> pages = labelItemService.selectLabelItemPage(Condition.getPage(query), labelItem);
		return R.data(pages);
	}

	/**
	 * 新增 标签
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入labelItem")
	public R save(@Valid @RequestBody LabelItem labelItem) {
		return R.status(labelItemService.save(labelItem));
	}

	/**
	 * 修改 标签
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入labelItem")
	public R update(@Valid @RequestBody LabelItem labelItem) {
		return R.status(labelItemService.updateById(labelItem));
	}

	/**
	 * 新增或修改 标签
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入labelItem")
	public R submit(@Valid @RequestBody LabelItem labelItem) {
		return R.status(labelItemService.saveOrUpdate(labelItem));
	}


	/**
	 * 删除 标签
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(labelItemService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 获取标签
	 *
	 * @return
	 */
	@GetMapping("/label")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "获取标签", notes = "获取标签")
	public R<List<LabelItem>> label(String type) {
		List<LabelItem> tree = labelItemService.getList(type);
		return R.data(tree);
	}
}
